package com.classeven.downhill.engine;

import java.util.Random;

import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;

import android.widget.Toast;

import com.classeven.downhill.define.iDefines;
import com.classeven.downhill.element.ASprite;


public class Utils 
{
	public static int GetRatio(float value)
	{

		switch (iDefines.RATIO_USE)
		{
			case iDefines.RATIO_WIDTH:
				return (int) (value / iDefines.MASTER_WIDTH * GameEngine.CAMERA_WIDTH);
			case iDefines.RATIO_HEIGHT:
				return (int) (value / iDefines.MASTER_HEIGHT * GameEngine.CAMERA_HEIGHT);
			default:
				return 0;
		}
	}
	
	public static int GetRatio(float master, float current, float value)
	{
		return (int) ((value/master) * current);
	}
	
	public static int GetRatioWidth(float value)
	{
		return (int) ((value/iDefines.MASTER_WIDTH) * GameEngine.CAMERA_WIDTH);
	}
	
	public static int GetRatioHeight(float value)
	{
		return (int) ((value/iDefines.MASTER_HEIGHT) * GameEngine.CAMERA_HEIGHT);
	}
	
	public static void TRACE(String somethingToTrace)
	{
		System.out.println(somethingToTrace);
	}
	
	public static int GetBigTwoInt(float value)
	{
		int _value = (int) value;
		int returnvalue = 0;
		for(int i = 1; i <= 12; i++)
		{
			int number = 1;
			for(int j=1; j <= i; j++)
			{
				number*=2;
			}
			if(_value < number)
			{
				returnvalue = number;
				break;
			}
		}
		return returnvalue;
	}
	
	public static int GetAnchorX(float width, Anchor anchor) 
	{
		float x;
		switch (anchor) 
		{
			case TOP_CENTER: 
			{
				x = (GameEngine.CAMERA_WIDTH / 2) - (width/2);
			}
			break;
			case TOP_RIGHT: 
			{
				x = GameEngine.CAMERA_WIDTH - width;
			}
			break;
			case CENTER: 
			{
				x = (GameEngine.CAMERA_WIDTH / 2) - (width/2);
			}
			break;
			case CENTER_RIGHT: 
			{
				x = GameEngine.CAMERA_WIDTH - width;
			}
			break;
			case BOTTOM_CENTER: 
			{
				x = (GameEngine.CAMERA_WIDTH / 2) - (width/2);
			}
			break;
			case BOTTOM_RIGHT: 
			{
				x = GameEngine.CAMERA_WIDTH - width;
			}
			break;
			case BOTTOM_MID_1: 
			{
				x = (GameEngine.CAMERA_WIDTH / 4) - (width/4);
			}
			break;
			case BOTTOM_MID_2: 
			{
				x = ((GameEngine.CAMERA_WIDTH / 4) * 3) - (3 * (width/4));
			}
			break;
			default: 
			{
				x = 0.0f;
			}
		}
		return (int) x;
	}

	public static int GetAnchorY(float height, Anchor anchor) 
	{
		float y;
		switch (anchor) 
		{
			case CENTER: 
			{
				y = (GameEngine.CAMERA_HEIGHT / 2) - (height/2);
			}
			break;
			case CENTER_LEFT: 
			{
				y = (GameEngine.CAMERA_HEIGHT / 2) - (height/2);
			}
			break;
			case CENTER_RIGHT: 
			{
				y = (GameEngine.CAMERA_HEIGHT / 2) - (height/2);
			}
			break;
			case BOTTOM_CENTER: 
			{
				y = GameEngine.CAMERA_HEIGHT - height;
			}
			break;
			case BOTTOM_LEFT: 
			{
				y = GameEngine.CAMERA_HEIGHT - height;
			}
			break;
			case BOTTOM_RIGHT: 
			{
				y = GameEngine.CAMERA_HEIGHT - height;
			}
			break;
			default:
			{
				y = 0.0f;
			}
		}
		return (int) y;
	}
	
	public static float getCenterObjX(Sprite spriteDestination, Sprite sprite)
	{
		// TODO Auto-generated method stub
		float x1 = spriteDestination.getX();
		
		float width = spriteDestination.getWidth();
		
		float width2 = sprite.getWidth();
	
		float centerX = (((x1 + width) - (x1 + width2)) / 2) +  x1;
		
		return centerX;
	}
	
	public static float getCenterObjY(Sprite spriteDestination, Sprite sprite)
	{
		float y1 = spriteDestination.getY();
		
		float heightY = spriteDestination.getHeight();
		float heightY2 = sprite.getHeight();

		float centerY = (((y1 + heightY) - (y1 + heightY2)) / 2) +  y1;
		
		return centerY;
	}
	
	public static boolean isOnArea(
			float p_X,
			float p_Y,
			Sprite p_Shape)
	{		
		return isOnArea(
				p_X,
				p_Y,
				p_Shape.getX(),
				p_Shape.getX() + p_Shape.getWidth(),
				p_Shape.getY(),
				p_Shape.getY() + p_Shape.getHeight());
	}
	
	public static boolean isOnArea(
			float p_X,
			float p_Y,
			Sprite p_ShapeParent,
			Sprite p_Shape)
	{		
		return isOnArea(
				p_X,
				p_Y,
				p_ShapeParent.getX() + p_Shape.getX(),
				p_ShapeParent.getX() + p_Shape.getX() + p_Shape.getWidth(),
				p_ShapeParent.getY() + p_Shape.getY(),
				p_ShapeParent.getY() + p_Shape.getY() + p_Shape.getHeight());
	}
	
	public static boolean isOnArea(
			float p_X,
			float p_Y,
			Sprite p_ShapeParent,
			Rectangle p_Rec,
			Sprite p_Shape)
	{		
		return isOnArea(
				p_X,
				p_Y,
				p_ShapeParent.getX() + p_Rec.getX() + p_Shape.getX(),
				p_ShapeParent.getX() + p_Rec.getX() + p_Shape.getX() + p_Shape.getWidth(),
				p_ShapeParent.getY() + p_Rec.getX() + p_Shape.getY(),
				p_ShapeParent.getY() + p_Rec.getX() + p_Shape.getY() + p_Shape.getHeight());
	}
	
	public static boolean isOnArea(
			float p_X,
			float p_Y,
			HUD p_ShapeParent,
			Sprite p_Shape)
	{		
		return isOnArea(
				p_X,
				p_Y,
				p_ShapeParent.getX() + p_Shape.getX(),
				p_ShapeParent.getX() + p_Shape.getX() + p_Shape.getWidth(),
				p_ShapeParent.getY() + p_Shape.getY(),
				p_ShapeParent.getY() + p_Shape.getY() + p_Shape.getHeight());
	}
	
	public static boolean isOnArea(
			float p_X,
			float p_Y,
			float p_MinX,
			float p_MaxX,
			float p_MinY,
			float p_MaxY)
	{
		if(p_X > p_MinX
				&& p_X < p_MaxX
				&& p_Y > p_MinY
				&& p_Y < p_MaxY)
			return true;
		
		return false;
	}
	
	public static boolean IsTouched_Move(ASprite sprite, TouchEvent pSceneTouchEvent)
	{
		if(((pSceneTouchEvent.getX() > sprite.theSprite().getX() && pSceneTouchEvent.getX() < sprite.theSprite().getX()+sprite.theSprite().getWidth())
				&&
		   (pSceneTouchEvent.getY() > sprite.theSprite().getY() && pSceneTouchEvent.getY() < sprite.theSprite().getY()+sprite.theSprite().getHeight()))
		   		&&
		   		pSceneTouchEvent.getAction() == TouchEvent.ACTION_MOVE
				)
		{
			return true;
		}
		return false;
	}
	
	public static boolean IsTouched_Down(ASprite sprite, TouchEvent pSceneTouchEvent)
	{
		if(((pSceneTouchEvent.getX() > sprite.theSprite().getX() && pSceneTouchEvent.getX() < sprite.theSprite().getX()+sprite.theSprite().getWidth())
				&&
		   (pSceneTouchEvent.getY() > sprite.theSprite().getY() && pSceneTouchEvent.getY() < sprite.theSprite().getY()+sprite.theSprite().getHeight()))
		   		&&
		   		pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN
				)
		{
			return true;
		}
		return false;
	}
	
	public static boolean IsTouched_Up(ASprite sprite, TouchEvent pSceneTouchEvent)
	{
		if(((pSceneTouchEvent.getX() > sprite.theSprite().getX() && pSceneTouchEvent.getX() < sprite.theSprite().getX()+sprite.theSprite().getWidth())
				&&
		   (pSceneTouchEvent.getY() > sprite.theSprite().getY() && pSceneTouchEvent.getY() < sprite.theSprite().getY()+sprite.theSprite().getHeight()))
		   		&&
		   		pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP
				)
		{
			return true;
		}
		return false;
	}
	
	

	
	public static int GetRandomInRange(int begin, int end)
	{
		if(begin > end)
		{
			System.out.println("end number shouldn't bigger that the beginning number");
			return -1;
		}
		Random randomGenerator = new Random();
		int range = end - begin;
		return begin + (randomGenerator.nextInt(range+1));
	}
	
	public static int GetRandomBetweenTwo(int first, int second)
	{
		Random random = new Random();
		boolean firstIndex = random.nextBoolean();
		if(firstIndex)
		{
			return first;
		}
		return second;
	}
	
	public static int GetRandomFromArray(int[]array)
	{
		Random random = new Random();
		int index = 0;
		try
		{
			index = random.nextInt(array.length);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return array[index];
	}
	
	public static void toast(final String pMessage)
	{
		GameEngine.context.runOnUiThread(new Runnable()
		{
			public void run()
			{
				Toast.makeText(GameEngine.context, pMessage, Toast.LENGTH_LONG).show();
			}
		});
	}
}
